-- 套装属性tips

local suitEquipsNum = 6 --套装组件个数
local suitEquipsAttrNum = 4 -- 套装提供的额外 属性/技能 条目数

local activeColor = cc.c3b( 165, 13, 244 ) -- 激活态颜色
local unactiveColor = cc.c3b( 68, 68, 68 ) -- 非激活态颜色

local FixedHeight = 0 -- 固定高度(除4条属性之外)

local distanceBetweenTextAndPaneltop = 0 -- 文本距panel上边距
local distanceBetweenTextAndPanelBtm = 0 -- 文本距panel下边距

local panelWidth = 0 -- panel宽
local panelHeight = 0 -- panel高

local distanceBetweenLine2AndTop = 0 -- line2 距离顶上 的间距
local distanceBetweenLine2AndPanel1 = 0 -- line2 和 Panel1 的Y坐标间距
local panelStartPosY = 0 -- 属性panel起始Y坐标

local utfCharNum = 10 -- 一行文本中包含的UTF8字符个数
local widthOfEachLine = 0 -- 一行文本宽度
local heightOfEachLine = 0 -- 一行文本高度


local string_split = string.split
local string_find = string.find
local string_sub = string.sub


local newSuitTipsView = class("newSuitTipsView",function()
    return ccui.Layout:create()
end)

function newSuitTipsView:ctor()
    local function createModuleNode(uiPath, uiNodeName, parent)
        local node = cc.uiloader:load( uiPath )
        local moduleNode = node:getChildByName( uiNodeName )
        moduleNode:retain()
        moduleNode:removeFromParent()
        parent:addChild( moduleNode )
        moduleNode:release()
        return moduleNode
    end
    self.mUINode = createModuleNode( Res.Tips_Suit, "Panel", self )
    local uiNodeSize = self.mUINode:getContentSize()
    self:setContentSize( uiNodeSize )
    ccui.Helper:doLayout( self.mUINode )

    distanceBetweenLine2AndTop = uiNodeSize.height - self.mUINode:getChildByName("line_2"):getPositionY()
    distanceBetweenLine2AndPanel1 = self.mUINode:getChildByName("line_2"):getPositionY()
                                        - self.mUINode:getChildByName("Panel_1"):getPositionY()
    distanceBetweenTextAndPaneltop = distanceBetweenLine2AndPanel1 - 2 -- 常量2 = line2高
    panelWidth, panelHeight = self.mUINode:getChildByName("Panel_1"):getContentSize2()
    distanceBetweenTextAndPanelBtm = self.mUINode:getChildByName("Panel_4"):getPositionY() - panelHeight
    for i = 1, suitEquipsAttrNum do
        display.setCascadeColorEnabled( self.mUINode:getChildByName("Panel_"..i), true )
    end
    FixedHeight = uiNodeSize.height - panelHeight * suitEquipsAttrNum
    widthOfEachLine, heightOfEachLine = self.mUINode:findChild("Panel_1/data_num"):getContentSize2()
end

-- 帮助函数
local function getEquipFromEquipsByLocation( equips, location )
    local equipTemp = nil
    for k,v in pairs( equips ) do
        if v:getPos() == location then
            equipTemp = v
            break
        end
    end
    return equipTemp
end

function newSuitTipsView:setSuitAttrTipsData( equipData, equips, specialistIdTemp ) -- 当前装备，身上装备，专精ID
    local suitId = equipData:getSuit()
    local suitConfig = GD:queryEquipSuitById( suitId )

    for i = 1, suitEquipsNum do
        self.mUINode:getChildByName("Text_"..i):setVisible( false )
        self.mUINode:getChildByName("Text_"..i):setTextColor( unactiveColor )
    end
    local suitName = L(suitConfig.name)

    local suitItemArr = string_split( suitConfig.string, ";" )
    local totalItemNumber = #suitItemArr -- 套装总件数

    local suitedEquipes = {} -- 身上的套装组件
    local quality = 1
    for i,v in ipairs( suitItemArr ) do
        local equipConf = GD:getEquipConf( v )
        quality = tonumber(equipConf.quality)
        assert( equipConf, " --->>> 套装组件中的一件装备不存在，装备ID：" .. v .. " ;套装Id：" .. suitId )

        self.mUINode:getChildByName("Text_"..i):setVisible( true )
        self.mUINode:getChildByName("Text_"..i):setString( L("location_" .. equipConf.location) )

        local equipedTemp = getEquipFromEquipsByLocation( equips, tonumber( equipConf.location ) )
        if equipedTemp and equipedTemp.tid == tonumber( v ) then -- pos上装备同tid
            self.mUINode:getChildByName("Text_"..i):setTextColor(Enums.QualityColor[quality])
            table.insert( suitedEquipes, equipConf )
        end
    end
    local totalEquipedItemNumber = #suitedEquipes -- 装备在身上的件数

    self.mUINode:getChildByName("tlt"):setString( table.concat( { suitName, " (",  totalEquipedItemNumber, "/", totalItemNumber, ")" } ) ) -- 套装名字
    self.mUINode:getChildByName("tlt"):setTextColor(Enums.QualityColor[quality])

    -- 套装属性
    local panelTotalHeight = 0 -- panel总高度
    local panelHeightList = {} -- 每一个panel的高度列表
    for i = 1, suitEquipsAttrNum do
        self.mUINode:getChildByName("Panel_"..i):setVisible( false )
        self.mUINode:getChildByName("Panel_"..i):setColor( unactiveColor )
    end
    for i = 1, suitEquipsAttrNum do
        if suitConfig["num" .. i] then -- 组件个数
            local str = string.format(L("lua_code_text_40"), suitConfig["num"..i])
            self.mUINode:findChild( table.concat( { "Panel_", i, "/data_tlt" } ) ):setString( str )

            local attrDescStr = ""
            local attrDescLineNum = 0 -- 套装属性描述行数
            -- 以下：增加的属性
            local specialistIDAttrList = string_split( suitConfig["propAdd"..i], ";" )
            local specialistIDAttrIdx = 1 -- 默认取第一个专精套装属性
            for i2,v2 in ipairs( specialistIDAttrList ) do
                local specialistIDEnd = string_find( v2, "|" )
                local specialistID = string_sub( v2, 1, specialistIDEnd - 1 )
                if tonumber( specialistID ) == specialistIdTemp then
                    specialistIDAttrIdx = i2
                    break
                end
            end
            local specialistIDAttrStr = specialistIDAttrList[ specialistIDAttrIdx ]
            local attrStr = string_split( specialistIDAttrStr, "|" )[2]
            local attrList = string_split( attrStr, "&" )
            local attrNum = #attrList
            -- print( " =========>>>>属性条目数 ", attrNum )
            for i3, v3 in ipairs( attrList ) do
                local attrPair = string_split( v3, ":" ) -- 属性对 { propId, value }
                local _, _, eachAttrDesc = UD:formatAttr( tonumber( attrPair[1] ), tonumber( attrPair[2] ) )
                attrDescStr = attrDescStr .. eachAttrDesc
                if i3 < attrNum then -- 非最后一行才需要补换行符
                    attrDescStr = attrDescStr .."\n"
                end
                attrDescLineNum = attrDescLineNum + 1
            end

            -- 以下：技能增强
            if suitConfig["classSkill"..i] then
                local specialistIDSkillList = string_split( suitConfig["classSkill"..i], ";" )
                for i2,v2 in ipairs( specialistIDSkillList ) do
                    local specialistIDEnd = string_find( v2, "|" )
                    local specialistID = string_sub( v2, 1, specialistIDEnd - 1 )
                    if tonumber( specialistID ) == specialistIdTemp then
                        specialistIDAttrIdx = i2
                        break
                    end
                end
                local specialistIDSkillStr = specialistIDSkillList[ specialistIDAttrIdx ]
                local classSkillId = string_split( specialistIDSkillStr, "|" )[2]
                local calssSkillConf = GD:getClassSkill( tonumber( classSkillId ) )

                attrDescStr = attrDescStr .. "\n" .. L(calssSkillConf.des)
                attrDescLineNum = attrDescLineNum + math.ceil( string.ulen( L(calssSkillConf.des) ) / utfCharNum )
            end

            -- print( " --------->>>>> 套装属性描述占用行数 " , i, attrDescLineNum, heightOfEachLine )


            local nodePath = table.concat( { "Panel_", i, "/data_num" } )
            self.mUINode:findChild( nodePath ):setContentSize( cc.size( widthOfEachLine, attrDescLineNum * heightOfEachLine ) )
            self.mUINode:findChild( nodePath ):setString( attrDescStr )

            local panelHeight =  distanceBetweenTextAndPaneltop + distanceBetweenTextAndPanelBtm
                                    + self.mUINode:findChild( nodePath ):getContentSize().height
            self.mUINode:getChildByName("Panel_"..i):setContentSize( cc.size( panelWidth, panelHeight ) )
            panelHeightList[i] = panelHeight
            panelTotalHeight = panelTotalHeight + panelHeight

            if "" ~= attrDescStr then
                self.mUINode:getChildByName("Panel_"..i):setVisible( true )
                if totalEquipedItemNumber >= tonumber( suitConfig["num" .. i] ) then
                    self.mUINode:getChildByName("Panel_"..i):setColor(Enums.QualityColor[quality])
                end
            end
        end
    end

    local totalHeight = FixedHeight + panelTotalHeight
    -- print( " --------->>>>> 最终总高度：" .. totalHeight .. " ;panel总高度：" .. panelTotalHeight )
    self.mUINode:setContentSize( cc.size( panelWidth, totalHeight ) )

    -- 调整属性panel的posY
    -- dump( panelHeightList, " --------->>>>> 每一个panel的高 " )
    panelStartPosY = totalHeight - distanceBetweenLine2AndTop - self.mUINode:getChildByName("line_2"):getContentSize().height - distanceBetweenLine2AndPanel1
    -- print( " ------->>>> panel起始Ypos " .. panelStartPosY .. " ;line2——POSY " .. self.mUINode:getChildByName("line_2"):getPositionY() )
    for i = 1, suitEquipsAttrNum do
        local upPanelHeight = 0
        for j = i-1, 1, -1 do
            local panelHeightTemp = 0
            if j < #panelHeightList then
                panelHeightTemp = panelHeightList[j]
            end
            upPanelHeight = upPanelHeight + panelHeightTemp
        end
        self.mUINode:getChildByName("Panel_"..i):setPositionY( panelStartPosY - upPanelHeight )
    end

    self:setContentSize( self.mUINode:getContentSize() )
    ccui.Helper:doLayout( self.mUINode )
end

return newSuitTipsView
